package com.rgs.platform.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.database.DataPageUtil;
import com.rgs.base.database.SqlPlusUtil;
import com.rgs.platform.entity.LendInfoDO;
import com.rgs.platform.filter.LendInfoFilterDTO;
import com.rgs.platform.mapper.LendInfoMapper;
import com.rgs.platform.service.LendInfoService;
import joptsimple.internal.Strings;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author Ren Gaoshuai
 * @ClassName LendInfoServiceImpl
 * @date 2022-11-30 11:17
 **/
@Service
public class LendInfoServiceImpl implements LendInfoService {

    @Resource
    LendInfoMapper lendInfoMapper;

    @Override
    public Optional<LendInfoDO> selectByFilter(LendInfoFilterDTO lendInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(LendInfoDO.class, lendInfoFilter);
        return lendInfoMapper.selectByExample(example).stream().findFirst();
    }

    @Override
    public List<LendInfoDO> listByFilter(LendInfoFilterDTO lendInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(LendInfoDO.class, lendInfoFilter);
        return lendInfoMapper.selectByExample(example);
    }

    @Override
    public PageInfo<LendInfoDO> listPageByFilter(IPageRequestModel iPageRequestModel, LendInfoFilterDTO lendInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(LendInfoDO.class, lendInfoFilter);
        return DataPageUtil.selectPageList(iPageRequestModel, () -> lendInfoMapper.selectByExample(example));
    }

    @Override
    public LendInfoDO selectById(String id) {
        return lendInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public LendInfoDO insert(LendInfoDO lendInfoDO) {
        if (Strings.isNullOrEmpty(lendInfoDO.getId())) {
            lendInfoDO.setId(IdUtil.fastSimpleUUID());
        }
        lendInfoDO.setCreateTime(LocalDateTime.now());
        lendInfoMapper.insert(lendInfoDO);
        return lendInfoDO;
    }

    @Override
    public void deleteByFilter(LendInfoFilterDTO lendInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(LendInfoDO.class, lendInfoFilter);
        lendInfoMapper.deleteByExample(example);
        return;
    }

    @Override
    public void deleteById(String id) {
        lendInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateSelectiveByPrimaryKey(LendInfoDO lendInfoDO) {
        lendInfoMapper.updateByPrimaryKeySelective(lendInfoDO);
    }
}
